/*
 *  Copyright (C) 2010 Glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.managers;

import java.util.Properties;
import javax.swing.JFormattedTextField;
import org.balisunrise.application.Application;
import org.balisunrise.common.BindingGroup;
import org.balisunrise.components.Converter;
import org.balisunrise.components.ComponentConverterException;
import org.balisunrise.components.ComponentManager;
import org.balisunrise.components.bindings.Binding;
import org.balisunrise.components.conveters.LongConverter;
import org.balisunrise.swing.components.util.BaliComponentFocusListener;
import org.balisunrise.swing.components.util.BaliPlainDocument;

/**
 *
 * @author glacuo
 */
public class LongComponentManager implements ComponentManager<Long, JFormattedTextField>{

    private static final LongConverter CONVERTER =
            new LongConverter();

    public Class<Long> getType() {
        return Long.class;
    }

    public Class<JFormattedTextField> getComponentType() {
        return JFormattedTextField.class;
    }

    public JFormattedTextField buildComponent(Properties p) {

        JFormattedTextField aux = new JFormattedTextField();

        aux.setFormatterFactory(
                new javax.swing.text.DefaultFormatterFactory(
                new javax.swing.text.NumberFormatter(
                new java.text.DecimalFormat("#0"))));
        aux.setFocusLostBehavior(JFormattedTextField.COMMIT);

        int length = 10;
        try{
            Object o = p.get("length");
            length = (Integer) o;
            if (length >= 19)
                length = 10;
        } catch(Exception e){}

        aux.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        aux.setDocument(BaliPlainDocument.createNegativesNumber(length));
        aux.setColumns(length);
        aux.addFocusListener(new BaliComponentFocusListener());

        //aux.setName(componentName);
        //aux.getAccessibleContext().setAccessibleName(componentName);

        return aux;
    }

    public void clearComponentValue(JFormattedTextField component) {
        if (null == component)
            return;
        if (component instanceof JFormattedTextField){
            component.setText("");
        }
    }

    public Object readValue(JFormattedTextField component) {
        
        if (null == component)
            return null;

        if (component instanceof JFormattedTextField){
            return ((JFormattedTextField) component).getText();
        }

        return null;
    }

    public Long readConvertedValue(JFormattedTextField component,
            boolean emptyForAll) throws ComponentConverterException {

        Object obj = readValue(component);

        if (obj == null)
            return null;

        String value = (String)obj;

        try{
            return CONVERTER.convertReverse(value);
        }catch(Exception e){
            throw new ComponentConverterException(e.getMessage());
        }
    }

    public void insertValue(Long propertyValue, JFormattedTextField component)
            throws ComponentConverterException {
        insertConvertedValue(CONVERTER.convertForward(propertyValue),component);
    }

    public void insertConvertedValue(Object propertyValue, JFormattedTextField component) {
        if (component == null)
            return ;
        component.setText((String) propertyValue);
    }

    public Converter<Long, ?> getConverter() {
        return CONVERTER;
    }

    public Binding createBinding(BindingGroup bindingGroup, Object source,
            String property, JFormattedTextField component) {

        return Application.get().getBindingBuilder().createBinding(
                bindingGroup,
                source,
                property,
                component,
                "text", // propriedade do componente a fazer o bind
                this,
                true,
                true);
    }
}
